package intelligentrecommender.phases.htmlextractor;

import com.google.gson.Gson;
import intelligentrecommender.phases.htmlextractor.SearchResults.Result;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;

public class FarroAPI {

    public FarroAPI(String query, int count) throws Exception {


        int length = count;
        String language = "en";
        String source = "web";
        query = query.replaceAll(" ", "+");

        String faroo = "http://www.faroo.com/instant.json?q=" + query + "&start=1&length=" + length + "&l=" + language + "&src=" + source;
        String charset = "UTF-8";
        boolean needsProxy = false;
        String proxyHost = "proxy";
        int proxyPort = 80;
        if (needsProxy) {
            System.setProperty("proxySet", "true");//true in home
            System.setProperty("http.proxyHost", "proxy");
            System.setProperty("http.proxyPort", "80");
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {

                    return new PasswordAuthentication("std\\120091149", "hany05941321".toCharArray());
                }
            });
        } else {
            System.getProperties().put("proxySet", "false");
            System.getProperties().put("proxyHost", "");
            System.getProperties().put("proxyPort", "");
        }
        URL url = new URL(faroo);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(2000);

        //Reader reader = new InputStreamReader(url.openStream(), charset);
        Reader reader = new InputStreamReader(connection.getInputStream(), charset);
        SearchResults results = new Gson().fromJson(reader, SearchResults.class);
        System.out.println(results.getResults().size());
        Iterator<Result> iter = results.getResults().iterator();
        int count1 = 0;
        // Create file 
        FileWriter fstream = new FileWriter("src/intelligentrecommender/phases/htmlextractor/results/google_search_engine_result.txt");
        BufferedWriter out = new BufferedWriter(fstream);
        String taitle, taitle1, taitle2, taitle3 = null;

        //while (iter.hasNext() && count1 < count) {
        while (iter.hasNext()) {
            Result r = iter.next();

            /*ArrayList<String> domainList = new ArrayList<>();
             domainList.add("www.insidefacebook.com");
             domainList.add("www.youtube.com");
             String urlStr = r.getUrl().toString();
             int slashslash = urlStr.indexOf("//") + 2;
             String domain = urlStr.substring(slashslash, urlStr.indexOf('/', slashslash));
             System.out.println(domain);
             if (!domainList.contains(domain)) {*/
            out.write(count1 + "");
            out.newLine();
            out.write(" Title : " + r.getTitle().toString());
            out.newLine();
            out.write(" Url : " + r.getUrl().toString());
            out.newLine();
            out.write("---------------------");
            out.newLine();
            System.out.println(count1);
            System.out.println(r.getTitle());
            System.out.println(r.getUrl());
            System.out.println("---------------------");
            taitle = r.getTitle().toString().replace("\"", "-");
            taitle1 = taitle.replace("'", "-");
            taitle2 = taitle1.replace("?", "-");
            taitle3 = taitle2.replace("!", "-");

            saveUrl(taitle3 + ".html", r.getUrl().toString());
            System.out.println("Done saving url ** ");

            count1++;
            //}
        }
        //Close the output stream
        out.close();
        reader.close();
    }

    public static void main(String[] args) {
        try {
            FarroAPI searcher3 = new FarroAPI("Google", 10);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void saveUrl(String filename, String urlString) throws MalformedURLException, IOException {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream("src/intelligentrecommender/phases/htmlextractor/results/" + filename);

            byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
    }
}
